/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

package br.ufmg.dees.insane.ui.rich.learn.learn_view.displacements_view.command;


import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.ArrayList;
import java.util.ListIterator;

import br.ufmg.dees.insane.draw.GaussPointDraw;
import br.ufmg.dees.insane.model.disc.element.ParametricElement;
import br.ufmg.dees.insane.model.disc.fem.FemModel;
import br.ufmg.dees.insane.model.disc.integrationpoint.IntegrationPoint;
import br.ufmg.dees.insane.model.disc.node.Node;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.CmdButton;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.LearnInterface;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.dialog.MatrixViewTableDialog;
import br.ufmg.dees.insane.util.IMatrix;

/**
*
* @author Moreira, Renata & Pitangueira, Roque
* @since May 2005
*/

public class MatricialShapeDialog {
	
	private static final long serialVersionUID = 1L;
	private LearnInterface parent;
	private FemModel fmodel;
	private MatrixViewTableDialog dlg;
	
	/**
	 * Constructor method, calling super with the especific title.
	 *
	 * @param parent The parent of the class.
	 * 
	 */
	public MatricialShapeDialog(LearnInterface parent) {
		this.parent = parent;
		fmodel = parent.getView().getController().getModel();
		IMatrix N;
		int ngdl = fmodel.getGlobalAnalysisModel().getNdf();
		dlg = new MatrixViewTableDialog("Shape Matrix", ngdl);
		
        //Number Format
		DecimalFormat fmt = new DecimalFormat();
        DecimalFormatSymbols fsym = new DecimalFormatSymbols();
        fsym.setDecimalSeparator('.');
        fmt.setDecimalFormatSymbols(fsym);
        fmt.applyPattern("0.000E00");
		
		ArrayList sel = this.parent.getView().getController().getSelection();
		ListIterator selLI = sel.listIterator();
		while(selLI.hasNext()){
		    GaussPointDraw ipd = (GaussPointDraw)selLI.next();
			IntegrationPoint ip = (IntegrationPoint)ipd.getRepresents();
			ParametricElement pelm = (ParametricElement)ipd.getOwner();
			N = pelm.mountN(ip);
			
			CmdButton[][] nMatrix = new CmdButton[N.getNumRow()][N.getNumCol()];
			//double[][] nMatrix = new double[N.getNumRow()][N.getNumCol()];
			for(int i =0; i< N.getNumRow(); i++)
				for(int j=0; j<N.getNumCol(); j++)
					nMatrix[i][j]=  new CmdButton(String.valueOf(fmt.format(N.getElement(i,j))));
			
			String[] colNames = new String[N.getNumCol()];
			int cont =0;
			for(int l=0; l<pelm.getIncidence().size(); l++){
			    String node = ((Node)pelm.getIncidence().get(l)).getLabel();
			    colNames[cont] = node;
			    colNames[cont+1] = node;
			    cont+=ngdl;
			}
			
			dlg.addOutputTable(pelm.getLabel()+"->"+ip.getLabel(),colNames,nMatrix);
		}
    }
		
    public int showDialog(){
    	return this.dlg.showDialog();
    }

}
